package com.ac.scum.log.web.model.wechat.mp.handler.msg;

import com.ac.scum.log.web.model.wechat.mp.builder.TextBuilder;
import com.ac.scum.log.web.model.wechat.mp.handler.AbstractHandler;
import com.ac.scum.log.web.model.wechat.mp.handler.msg.stage.KeywordMsgStage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@AllArgsConstructor
public class MsgHandler extends AbstractHandler {

    private final KeywordMsgStage keywordMsgStage;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) throws WxErrorException {
        if (wxMessage.getContent() != null) {
            if (wxMessage.getContent().equalsIgnoreCase("授权")) {
                String resultAuthUri = weixinService.getOAuth2Service().buildAuthorizationUrl("http://api.scum-log.cn/wechat/redirect/greet", "snsapi_userinfo", "state");
                log.info(resultAuthUri);
                return new TextBuilder().build(resultAuthUri, wxMessage, weixinService);
            }
            String keywordReply = keywordMsgStage.getKeywordReply(wxMessage.getContent());
            if (StringUtils.isNotBlank(keywordReply)) {
                return new TextBuilder().build(keywordReply, wxMessage, weixinService);
            }
        }
        return new TextBuilder().build("日志平台：scum-log.cn有问题可联系开发者V:acbcl-cn,Q:2597400564", wxMessage, weixinService);
    }


}
